﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace testgame
{
    class Bullet
    {
        //绘制子弹
        private int x;//子弹横坐标
        private int y;//子弹纵坐标
        private const int step = 10;//子弹移动速度
        private const int BulletWidth = 10;//子弹宽度
        public int Angle;//子弹角度
        public Rectangle Rectangle { get; set; }//声明子弹所在矩形
        private const int SpeedY = 10;//子弹速度
        private Image bulImg;//定义子弹图片
        private const double PI = Math.PI;//产生不同方向的子弹
        public bool isHit = false;//碰撞的标志
        public static List<Bullet> bulList = new List<Bullet>();//子弹对象集合

        public Bullet()
        {   
        }

        public Bullet (int bx,int by,int angle)
        {
            //给子弹x、y、角度、所在矩形赋值
            x = bx;
            y = by;
            Angle = angle;
            Rectangle = new Rectangle(bx, by, 10, 10);

            //随机产生不同方向的子弹
            switch (Angle)
            {
                case 0:
                    bulImg = Properties.Resources.子弹;
                    y -= 17;
                    break;
                case 30:
                    bulImg = Properties.Resources.子弹30;
                    x += 12;
                    y -= 12;
                    break;
                case 60:
                    bulImg = Properties.Resources.子弹60;
                    x += 2;
                    y -= 17;
                    break;
                case 120:
                    bulImg = Properties.Resources.子弹120;
                    x -= 35;
                    y -= 12;
                    break;
                case 150:
                    bulImg = Properties.Resources.子弹150;
                    x -= 20;
                    y -= 12;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 通过按键盘J键来产生我方子弹
        /// </summary>
        public static void ProduceMybul()
        {
            if (LaunchCanister.IsKeyDown (Keys.J))
            {
                bulList.Add(new Bullet(LaunchCanister.LCx + 13, LaunchCanister.LCy - 10, 0));
                //bulList.Add(new Bullet(LaunchCanister.LCx + 13, LaunchCanister.LCy - 8, 60));
                //bulList.Add(new Bullet(LaunchCanister.LCx + 7, LaunchCanister.LCy - 8, 30));
                //bulList.Add(new Bullet(LaunchCanister.LCx + 30, LaunchCanister.LCy - 12, 120));
                //bulList.Add(new Bullet(LaunchCanister.LCx, LaunchCanister.LCy - 7, 150));

            }
        }
        //画子弹
        public void ShowMybul(Graphics e)
        {
            e.DrawImage(bulImg, new Point(x, y));
        }

        //子弹的移动
        public static void MoveMybul(Graphics g)
        {
            for (int i = 0; i < bulList.Count; i++)
            {
                bulList[i].ShowMybul(g);
                switch (bulList[i].Angle)
                {
                    case 0:
                        bulList[i].y -= SpeedY;
                        break;
                    case 30:
                        bulList[i].x += (int)(SpeedY / 2);
                        bulList[i].y -= (int)(SpeedY * Math.Cos(PI / 6));
                        break;
                    case 60:
                        bulList[i].x += (int)(SpeedY  * Math.Cos(PI / 6));
                        bulList[i].y -= (int)(SpeedY / 2);
                        break;
                    case 120:
                        bulList[i].x -= (int)(SpeedY  / 2);
                        bulList[i].y -= (int)(SpeedY  * Math.Cos(PI / 6));
                        break;
                    case 150:
                        bulList[i].x -= (int)(SpeedY  * Math.Cos(PI / 6));
                        bulList[i].y -= (int)(SpeedY  / 2);
                        break;
                }
                if (bulList[i].y < 0 || bulList[i].x > 490 || bulList[i].x < 0)
                {
                    bulList.Remove(bulList[i]);
                }
            }
        }
        /// <summary>
        /// 怪兽碰撞检测方法
        /// </summary>
        public static void IsHitEnemy(Graphics g)
        {
            Rectangle lcRect = new Rectangle(LaunchCanister .LCx , LaunchCanister .LCy , LaunchCanister .LCImage .Width, LaunchCanister .LCImage .Height);    //包住myplane的Rectangle

            //g.DrawRectangle(new Pen(Color.Red), lcRect);
            for (int i = 0; i < bulList.Count; i++)
                for (int j = 0; j < monster.monsters.Count; j++)
                {

                    Rectangle bulRect = new Rectangle(bulList[i].x, bulList[i].y, 44, 45);           //子弹所在矩形
                    Rectangle mosRect = new Rectangle(monster.monsters[j]._x, monster .monsters[j]._y, 100, 100);     //怪兽所在矩形
                    
                    //g.DrawRectangle(new Pen(Color.Black), mosRect);
                    if (bulRect.IntersectsWith(mosRect))   //我方子弹击中怪兽，子弹消失，怪兽消失，产生爆炸效果，得分+1
                    {
                        bulList.Remove(bulList[i]);                   //子弹消失
                        monster.monsters.Remove(monster.monsters[j]); //怪兽消失
                        g.DrawImage(Properties.Resources.boom, mosRect);//画爆炸效果
                        SoundPlayer music = new SoundPlayer(Properties.Resources.boom1);//爆炸音效
                        music.Play();
                        //monster.monsters[j].flag = true;
                        if (LaunchCanister.score < 100)    //分数+1  
                        {
                            LaunchCanister.score += 1;
                        }
                    }
                   
                }



        }
    }
}
